import httpx
import argparse
import hashlib

def encrypt_password(password: str) -> str:
    return hashlib.sha256(password.encode()).hexdigest()


def run(
    url: str,
    name: str,
    email: str,
    password: str,
    phone: str,
    role: int,
):
    data = {
        "name": name,
        "email": email,
        "password": encrypt_password(password),
        "phone": phone,
        "role": role,
    }
    response = httpx.post(url, json=data)
    if response.status_code != 200:
        raise Exception(response.status_code, response.text)
    
    print(response.json())
    return response.json()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--url", type=str, help="request url", default="http://127.0.0.1:18000/user/create")
    parser.add_argument("--name", type=str, required=True, help="user name")
    parser.add_argument("--email", type=str, required=True, help="user email")
    parser.add_argument("--password", type=str, required=True, help="user password")
    parser.add_argument("--phone", type=str, help="user phone")
    parser.add_argument("--role", type=int,  help="user role", default=0, choices=[0, 1, 2])
    args = parser.parse_args()

    run(
        args.url,
        args.name,
        args.email,
        args.password,
        args.phone,
        args.role,
    )


if __name__ == "__main__":
    main()
